const mongoose = require("mongoose");

class DatabaseSingleCase {
  protected instance: any;

  public async createInstance() {
    if (!this.instance) {
      try {
        this.instance = await mongoose.connect(process.env.MONGODB_URL_TEST, {
          useNewUrlParser: true,
        });
        return this.instance;
      } catch (e) {
        return null;
      }
    }
    return this.instance;
  }

  public getInstance() {
    return this.instance;
  }

}


export const createDatabaseInstance = async () => {
  const database = new DatabaseSingleCase();
  return await database.createInstance();
};

